<?php

namespace App\Service;

use App\Models\EwNodes;
use App\Models\EwRoles;
use App\Models\EwUser;

class NodesService extends BaseService
{
	const DISPLAY_TRUE = 1;		//显示标识：是
	const DISPLAY_FALSE = 0;	//显示标识：否
	const MENU_TRUE = 1;		//删除标识：是
	const MENU_FALSE = 0;		//删除标识：否

	public static function getList($params, $count = FALSE)
	{
		$query = EwNodes::query();
		$query->columns(['id,pid,name,is_display,url,api_url,is_menu,order_by,update_time,create_time']);

		//通过名称
		if (isset($params->name) && $params->name)
			$query->andwhere("name LIKE '%" . $params->name . "%'");

		//通过名称
		if (isset($params->url) && $params->url)
			$query->andwhere("url LIKE '%" . $params->url . "%'");

		//通过pid
		if (isset($params->pid) && $params->pid)
			$query->andwhere("pid = " . $params->pid);

		//总条数
		if ($count)
			return $query->execute()->count();

		//order_by 排序
		if (isset($params->order_by)){
			$sql = parent::orderBy($params->order_by);
			$query->orderBy($sql);
		}else{
			$query->orderBy("order_by ASC");
		}

		//分页
		$query->limit($params->page_num,($params->p-1)*$params->page_num);

		return $query->execute();
	}

	public static function getInfo($id = 0)
	{
		return EwNodes::findFirst($id);
	}

	public static function count()
	{
		return EwNodes::count();
	}

	/**
	 * [display 上架]
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public static function display($id)
	{
		if (isset($id) && $id)
			$data = EwNodes::findFirst($id);

		if ($data){
			$data->is_display = $data->is_display ? self::DISPLAY_FALSE : self::DISPLAY_TRUE;
			return $data->save();
		}

		return NULL;
	}

	/**
	 * [save 保存数据]
	 * @param  [type] $data [description]
	 * @return [type]       [description]
	 */
	public static function save($data)
	{
		$row = new EwNodes;
		if (isset($data->id) && $data->id)
			$row = EwNodes::findFirst($data->id);

		return $row->save((array)$data);
	}

	/**
	 * [delete 删除数据]
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public static function forever($id)
	{
		//如果是某个数据的父级
		if (isset($id) && $id)
			$row = EwNodes::findFirst(['conditions'=> 'pid =' . $id]);
		//不允许删除
		if ($row)
			return FALSE;

		if (isset($id) && $id)
			$row = EwNodes::findFirst($id);

		if ($row)
			return $row->delete();

		return NULL;
	}

	/**
	 * [getTree 获取树形结构]
	 * @param  integer $cate_id [cats外键]
	 * @return [type]           [description]
	 */
	public static function getTree($params)
	{

        $data = EwNodes::find(["columns"=>"id,pid,name,url","conditions" => "is_display = " . self::DISPLAY_TRUE . " AND is_menu = " . self::MENU_TRUE])->toArray();

		//获取无限极分类树
		$cate = new \App\Util\Category($data);
		return $cate::tree('children');
	}

    /**
     * [getTreeNode 获取树形结构]
     * @param  integer $cate_id [cats外键]
     * @return [type]           [description]
     */
    public static function getTreeNode($params)
    {

        $data = EwNodes::find(["columns"=>"id,pid,name,url","conditions" => "is_display = " . self::DISPLAY_TRUE . " AND is_menu = " . self::MENU_TRUE
            ." AND FIND_IN_SET(id,(SELECT menu_nodes from  [App\\Models\\EwRoles] nr where nr.id={$params['roleid']} ))>0"])->toArray();

        //获取无限极分类树
        $cate = new \App\Util\Category($data);
        return $cate::tree('children');
    }

	/**
	 * [getchildren 数组转树形结构]
	 * @param  [type]  $list [description]
	 * @param  integer $pid  [description]
	 * @return [type]        [description]
	 */
 	/*private static function getchildren(& $list, $pid = 0) 
 	{
	    $arr = array();
	    foreach ($list as $row) {
	        if ($row['pid'] == $pid) {
	            $arr[$row['id']] = $row;
	            $children = self::getchildren($list, $row['id']);
	            $children = array_values($children);
	            $children && $arr[$row['id']]['children'] = $children;
	        }
	    }
	    $arr = array_values($arr);
	    return $arr;
	}*/
}